package com.design.twords.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.design.twords.MainActivity;
import com.design.twords.R;
import com.design.twords.config.ConfigData;
import com.design.twords.controller.NumberController;
import com.design.twords.database.User;
import com.design.twords.database.UserData;
import com.design.twords.utils.MyApplication;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Create by Alen
 * Time:  16:16
 */
public class RegisterActivity extends AppCompatActivity {
    private String usn,pwd;
    private SharedPreferences spre;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        MyApplication.getInstance().addActivity(this);

        EditText et_usn = findViewById(R.id.et_usn);
        EditText et_pwd = findViewById(R.id.et_pwd);
        spre = getSharedPreferences("user_details",MODE_PRIVATE);

        et_usn.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String editable = et_usn.getText().toString();
                String regEx = "[^a-zA-Z0-9]";  //只能输入字母或数字
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(editable);
                String str = m.replaceAll("").trim();    //删掉不是字母或数字的字符
                if(!editable.equals(str)){
                    et_usn.setText(str);  //设置EditText的字符
                    et_usn.setSelection(str.length()); //因为删除了字符，要重写设置新的光标所在位置
                }

            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        et_pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String editable = et_pwd.getText().toString();
                String regEx = "[^a-zA-Z0-9]";  //只能输入字母或数字
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(editable);
                String str = m.replaceAll("").trim();    //删掉不是字母或数字的字符
                if(!editable.equals(str)){
                    et_pwd.setText(str);  //设置EditText的字符
                    et_pwd.setSelection(str.length()); //因为删除了字符，要重写设置新的光标所在位置
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        Button bt_regis = findViewById(R.id.bt_register);

        bt_regis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                usn = et_usn.getText().toString().trim();
                pwd = et_pwd.getText().toString();

                if(usn.equals("")||pwd.equals("")){
                    Toast.makeText(RegisterActivity.this,"用户名或密码不能为空",Toast.LENGTH_LONG).show();
                }else if(4 >= usn.length() || usn.length() >= 18){
                    Toast.makeText(RegisterActivity.this,"用户名长度应在4-18之间",Toast.LENGTH_LONG).show();
                }
                else{
                    User user = new User();
                    user.setUsername(usn);
                    user.setPassword(pwd);
                    user.setNickName("TWords No."+ NumberController.getRandomNumber(1,999));
                    user.signUp(new SaveListener<User>() {
                        @Override
                        public void done(User user, BmobException e) {
                            if(e==null){
                                Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                                SharedPreferences.Editor editor = spre.edit();
                                editor.putString("username",usn);
                                editor.putString("password",pwd);
                                editor.apply();

                                ConfigData.setUserId(user.getObjectId());
                                UserData userData = new UserData();
                                userData.setUserId(user.getObjectId());
                                userData.setNickName(user.getNickName());
                                userData.save();

                                startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                                finish();
                            }else{
                                Toast.makeText(RegisterActivity.this,"注册失败 该用户名已存在",Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                }

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().removeActivity(this);
    }
}
